package com.test.file;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BatchSql2Single {

    private static final String FILE_SUFFIX = ".sql";

    private static final String FILE_PREFIX = "20220228_";

    private static final Integer HANDLE_NUMBER = 30000;

    private static final String TEMP_DIRECTORY = "D:\\";

    private static final String DIRECTORY_PATH = "C:\\Users\\47483\\Documents\\WeChat Files\\wxid_9750587505912\\FileStorage\\File\\2022-02\\fengye_ota";

    public static void main(String[] args) throws Exception {

    }

    private static void test1() {
        // 读取所有路径下文件列表
        File directory = new File(DIRECTORY_PATH);
        List<File> inFileList = new ArrayList<>();
        if (directory.exists() && directory.isDirectory()) {
            Arrays.stream(directory.listFiles()).filter(s -> s.getName().contains(".sql")).forEach(s -> {
                inFileList.add(s);
            });
        }
        //
        int handleNumber = 0;
        // 计算所有行数
        for (int i = 0; i < inFileList.size(); i++) {
            File f = inFileList.get(i);
            try (BufferedReader reader = new BufferedReader(new FileReader(f))) {
                while(reader.readLine() != null) {
                    handleNumber++;
                }
                handleNumber++;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //


    }


    private static void test() throws Exception {
        String dirName = "C:\\Users\\47483\\Documents\\WeChat Files\\wxid_9750587505912\\FileStorage\\File\\2022-02\\fengye_ota";
        String outFileName = "D:\\20220228.sql";
        // 读取所有路径下文件列表
        File directory = new File(dirName);
        List<File> inFileList = new ArrayList<>();
        if (directory.exists() && directory.isDirectory()) {
            Arrays.stream(directory.listFiles()).filter(s -> s.getName().contains(".sql")).forEach(s -> {
                inFileList.add(s);
            });
        }
        File outFile = new File(outFileName);
        if (!outFile.exists()) {
            outFile.createNewFile();
        }
        //
        int num = 0;
        // 创建输出文件
        try (BufferedWriter out = new BufferedWriter(new FileWriter(outFile))) {
            for (File f : inFileList) {
                try (BufferedReader reader = new BufferedReader(new FileReader(f));) {
                    String s = "";
                    while ((s = reader.readLine()) != null) {
                        out.write(s);
                        num++;
                    }
                    out.newLine();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
